// pages/activityCenter/jf1.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "交房预约", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    // 姓名
    name: '',
    // 电话
    phone: '',
    //组团
    group: '请选择组团',
    groupList: [{
      id: 0,
      name: '麓悦江城'
    }, {
      id: 1,
      name: '麓溪半岛'
    }],
    //房号
    roomNum: '请选择房号',
    roomNumList: [{
      id: 0,
      name: '1栋'
    }, {
      id: 1,
      name: '2栋'
    }, {
      id: 2,
      name: '3栋'
    }, {
      id: 3,
      name: '4栋'
    }],

    openid: '',
    shareTitle: '',
    shareImg: '',
    tipsContent: '',
    id: 0,
    activityInfo: [],
    is_register: 0
  },
  // 姓名
  nameIpt(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 电话
  phoneIpt(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //组团
  groupChange(e) {
    let that = this;
    that.setData({
      group: that.data.groupList[e.detail.value].name,
    })
  },
  //房号
  roomNumChange(e) {
    let that = this;
    that.setData({
      roomNum: that.data.roomNumList[e.detail.value].name,
    })
  },
  //点击验证
  yz() {
    let that = this;
    //判断是否注册
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.redirectTo({
              url: ''
            })
          }
        }
      })
      return;
    }

    if (that.data.name.trim() === '') {
      app.showTip('请输入姓名');
    } else if (that.data.phone.trim() === '') {
      app.showTip('请输入电话');
    } else if (isNaN(that.data.phone) || that.data.phone.length !== 11) {
      app.showTip('请输入正确电话');
    } else if (that.data.group.trim() === '请选择组团') {
      app.showTip('请选择组团');
    } else if (that.data.roomNum.trim() === '请选择楼栋') {
      app.showTip('请选择楼栋');
    } else {
      wx.showLoading({
        title: '数据认证中...'
      });

      wx.request({
        url: that.data.baseUrl + 'checkJfInfo.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          name: that.data.name.trim(),
          tel: that.data.phone.trim(),
          team: that.data.group.trim(),
          room_number: that.data.roomNum.trim(),
          id: that.data.id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          wx.hideLoading();
          app.showTip(res.data.msg);
          if (res.data.status == 1) {
            setTimeout(function() {
              wx.navigateTo({
                url: '/pages/activityCenter/jf2?id=' + that.data.id + "&team=" + that.data.group.trim() + "&room_number=" + that.data.roomNum.trim()
              })
            }, 1500);
          }
        },
        fail: function(res) {
          wx.hideLoading();
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var id = options.id;
    //id = 1;
    if (id == '' || id == '0') {
      app.showTip('抱歉,未获取到相关的id信息哦');
      setTimeout(function() {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
      return;
    }
    this.setData({
      id: id
    });

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getActiveJf1PageInfo(res.data.openid, id);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getActiveJf1PageInfo(openid, id);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getActiveJf1PageInfo(openid, id) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getActiveJf1PageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'id': id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            is_register: res.data.is_register,
            tipsContent: res.data.tipsContent,
            activityInfo: res.data.activityInfo,
            shareTitle: res.data.activityInfo.title,
            shareImg: that.data.baseUrl + 'admin/' + res.data.activityInfo.img_path
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });

  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  //跳转业主中心认证
  toCheckYzPage() {
   wx.navigateTo({
     url: '/pages/my/ownerValidate'
   });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;

    return {
      title: that.data.shareTitle,
      path: '/pages/activityCenter/detail?type=1' + "&id=" + that.data.id + '&is_share=1',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})